# Maintainer: Andrew Sun <adsun701@gmail.com>

_realname=alembic
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.7.12
pkgrel=2
pkgdesc="A open framework for storing and sharing scene data (mingw-w64)"
arch=('any')
url="https://www.alembic.io/"
license=('BSD3')
depends=("${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-boost")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/alembic/${_realname}/archive/${pkgver}.tar.gz"
        "0001-alembic-1.7.9-fix-windows-build.patch"
        "0002-alembic-1.7.9-remove-msc_ver-and-fix-builtins-and-includes.patch")
sha512sums=('e05e0b24056c17f01784ced1f9606a269974de195f1aca8a6fce2123314e7ee609f70df77ac7fe18dc7f0c04fb883d38cc7de9b963caacf9586aaa24d4ac6210'
            'e06598a65b61c89b2e7a4403abfef0ab763c8579e901382289c22a0418ce26408df64703b6e30ac380fa8c7f2a0ad2a97b69b6d5b1b8cf598d135cef9ee3afe1'
            'e3f69356519d000004d68836c09340a78830c671f00bd9e317d798fa91b72c4d879e938ab2fc1e935775c193b9622dca2c42327aff5ee3d0e185e3a1a402a9d9')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -Np1 -i "${srcdir}/0001-alembic-1.7.9-fix-windows-build.patch"
  patch -Np1 -i "${srcdir}/0002-alembic-1.7.9-remove-msc_ver-and-fix-builtins-and-includes.patch"
}

build() {
  # Static Build
  cd "${srcdir}"/${_realname}-${pkgver}
  [[ -d "${srcdir}"/build-${CARCH}-static ]] && rm -rf "${srcdir}"/build-${CARCH}-static
  mkdir -p "${srcdir}"/build-${CARCH}-static && cd "${srcdir}"/build-${CARCH}-static

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi
  
  export ILMBASE_ROOT=${MINGW_PREFIX}
  export OPENEXR_ROOT=${MINGW_PREFIX}
  export PYILMBASE_ROOT=${MINGW_PREFIX}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DUSE_HDF5=ON \
      -DALEMBIC_ILMBASE_LINK_STATIC=OFF \
      -DALEMBIC_SHARED_LIBS=0 \
      ../${_realname}-${pkgver}

  make
  
  # Shared Build
  cd "${srcdir}"/${_realname}-${pkgver}
  [[ -d "${srcdir}"/build-${CARCH}-shared ]] && rm -rf "${srcdir}"/build-${CARCH}-shared
  mkdir -p "${srcdir}"/build-${CARCH}-shared && cd "${srcdir}"/build-${CARCH}-shared

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DUSE_HDF5=ON \
      -DUSE_PYALEMBIC=OFF \
      -DALEMBIC_SHARED_LIBS=1 \
      ../${_realname}-${pkgver}

  make
}

package() {
  
  # Static Install
  cd "${srcdir}"/build-${CARCH}-static
  make install DESTDIR="${pkgdir}"

  # Shared Install
  cd "${srcdir}"/build-${CARCH}-shared
  make install DESTDIR="${pkgdir}"

  local WINPREFIX=$(cygpath -wm ${MINGW_PREFIX})
  # Fix paths
  sed -i "s|${WINPREFIX}|${MINGW_PREFIX}|g" \
    ${pkgdir}${MINGW_PREFIX}/lib/cmake/Alembic/AlembicTargets.cmake \
    ${pkgdir}${MINGW_PREFIX}/lib/cmake/Alembic/AlembicTargets-release.cmake
  
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE.txt "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE
}
